﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Project.Models;
using Project.Database;
using Project.Database.Repository;
using Project.Implementations;
using System.IO;

namespace Project.Controllers
{
    public class ProfileController : Controller
    {
        private DatabaseRepository entities = new DatabaseRepository();
        //
        // GET: /Profile/
        [Authorize]
        public ActionResult Index()
        {
            User user = entities.GetUser(User.Identity.Name);
            Role role = entities.GetRoleForUser(user);
            if (role.ID == 7) // blogger
            {
                ViewBag.MemberProfile = true;
                ViewBag.Blogs = entities.GetBlogsForUser(user);
                return View(user);
            }
            if (role.ID == 6) //advetiser
            {
                ViewBag.MemberProfile = false;
                Advertiser advertiser = entities.GetAdvertiser(user);
                if (advertiser.BrandName == null)
                {
                    return RedirectToAction("CreateAdvertiserProfile");
                }
                //ViewBag.Adverts = entities.GetAdvertsForAdvertiser(advertiser);
                return View(advertiser);
            }
            return View();
        }

        [Authorize(Roles = "Blogger")]
        public ActionResult CreateUserProfile()
        {
            return View();
        }

        //
        // POST: /Account/Register

        [HttpPost]
        [Authorize(Roles = "Blogger")]
        public ActionResult CreateUserProfile(UserProfileModel model, HttpPostedFileBase file)
        {
            if (ModelState.IsValid)
            {
                try
                {
                    string path = "~/uploads/profiles/default.png";
                    if (file != null && file.ContentLength > 0)
                    {
                        User user = entities.GetUser(User.Identity.Name);
                        // extract only the fielname
                        string fileName = user.ID + "_" + user.UserName + "_" + "profile";
                        // store the file inside ~/uploads/logos/ folder
                        path = Path.Combine("~/uploads/profiles/", fileName);
                        file.SaveAs(Server.MapPath(path));
                    }
                    //entities.UpdateAdvertiser(User.Identity.Name, model.BrandName, model.Description, path, model.Website, model.FirstName, model.LastName, model.Address);
                    return RedirectToAction("Index", "Profile");
                }
                catch (ArgumentException ae)
                {
                    ModelState.AddModelError("", ae.Message);
                }
            }

            // If we got this far, something failed, redisplay form
            return View();
        }


        [Authorize(Roles = "Advertiser")]
        public ActionResult CreateAdvertiserProfile()
        {
            return View();
        }

        //
        // POST: /Account/Register

        [HttpPost]
        [Authorize(Roles = "Advertiser")]
        public ActionResult CreateAdvertiserProfile(AdvertiserModel model, HttpPostedFileBase file)
        {
            if (ModelState.IsValid)
            {
                try
                {
                    string path = "~/uploads/logos/default.png";
                    if (file != null && file.ContentLength > 0)
                    {
                        Advertiser advertiser = entities.GetAdvertiser(entities.GetUser(User.Identity.Name));
                        // extract only the fielname
                        string fileName = advertiser.ID + "_logo_" + file.FileName.Trim().Replace(' ', '_');
                        // store the file inside ~/uploads/logos/ folder
                        path = Path.Combine("~/uploads/logos/", fileName);
                        file.SaveAs(Server.MapPath(path));
                    }
                    entities.UpdateAdvertiser(User.Identity.Name, model.BrandName, model.Description, path, model.Website, model.Address);
                    return RedirectToAction("Index", "Profile");
                }
                catch (ArgumentException ae)
                {
                    ModelState.AddModelError("", ae.Message);
                }
            }

            // If we got this far, something failed, redisplay form
            return View();
        }

        [Authorize(Roles = "Advertiser")]
        public ActionResult CreateAdCampaign()
        {
            return View();
        }

        [HttpPost]
        [Authorize(Roles = "Advertiser")]
        public ActionResult CreateAdCampaign(AdCampaignModel model)
        {
            if (ModelState.IsValid)
            {
                try
                {
                    User user = entities.GetUser(User.Identity.Name);
                    Advertiser advetiser = entities.GetAdvertiser(user);
                    entities.CreateCampaign(advetiser.ID, model.CampaignName, model.StartDate, model.EndDate, model.StartingBalance);
                    return RedirectToAction("Index", "Profile");
                }
                catch (ArgumentException ae)
                {
                    ModelState.AddModelError("", ae.Message);
                }
            }
            return View();
        }

        [Authorize(Roles = "Advertiser")]
        public ActionResult ShowCampaign(int id)
        {
            Advertiser advetiser = entities.GetAdvertiser(entities.GetUser(User.Identity.Name));
            AdCampain campaign = entities.GetAdCampaignForAdvertiser(advetiser, id);
            if (campaign == null)
            {
                return RedirectToAction("InvalidCampaign", "Error");
            }
            IQueryable<Advert> ads = entities.GetAdvertsForCampaign(campaign);
            ViewBag.Adverts = ads;

            return View(campaign);
        }
    }
}
